﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Data.Common;

namespace QUANLYNHASACH.App_code.BLL
{
    class Group_BLL
    {
        public List<QUANLYNHASACH.Group_td> LayDanhSachGroup()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "getGroup";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader = cmd.ExecuteReader();

                    List<QUANLYNHASACH.Group_td> GroupLst = new List<QUANLYNHASACH.Group_td>();

                    while (reader.Read())
                    {
                        QUANLYNHASACH.Group_td Grp = new QUANLYNHASACH.Group_td();
                        Grp.idGroup = reader[0].ToString();
                        Grp.CapBac = reader[1].ToString();
                        GroupLst.Add(Grp);
                    }
                    conn.Close();
                    return GroupLst;
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra: " + ex.Message, "Thông báo lỗi");
            }
            return null;

        }
        public void CapNhatGroup(string idGroup, string CapBac)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "editGroup";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idGroup";
                    param.Value = idGroup;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@CapBac";
                    param.Value = CapBac;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
        ////////
        public void ThemGroup(string idGroup, string CapBac)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "addGroup";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idGroup";
                    param.Value = idGroup;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@CapBac";
                    param.Value = CapBac;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }

        }
        public void XoaGroup(string idGroup)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delGroup";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idGroup";
                    param.Value = idGroup;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
    }
}
